package com.cloudstreamservice8002.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudstreamservice8002.entity.DeliveryInfo;
import com.cloudstreamservice8002.mapper.DeliveryInfoMapper;
import com.cloudstreamservice8002.param.DeliverInfoCreatOrUpdateParam;
import com.cloudstreamservice8002.service.DeliveryInfoService;
import com.cloudstreamservice8002.util.SnowflakeIdGenerator;
import com.google.common.base.Preconditions;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 26407
 * @description 针对表【delivery_info】的数据库操作Service实现
 * @createDate 2025-06-11 00:42:49
 */
@Service
public class DeliveryInfoServiceImpl extends ServiceImpl<DeliveryInfoMapper, DeliveryInfo>
        implements DeliveryInfoService {

    @Resource
    private DeliveryInfoMapper deliveryInfoMapper;

    @Override
    public IPage<DeliveryInfo> getDeliveryInfoList(Integer pageNum, Integer pageSize) {
        Page<DeliveryInfo> page = new Page<>(pageNum, pageSize);
        return deliveryInfoMapper.selectPage(page, null);
    }

    @Override
    public Boolean create(DeliverInfoCreatOrUpdateParam deliveryInfo) {
        //校验参数
        Preconditions.checkArgument(deliveryInfo.getSenderAddress() != null, "发货地址不能为空");
        Preconditions.checkArgument(deliveryInfo.getReceiverAddress() != null, "收货地址不能为空");
        Preconditions.checkArgument(deliveryInfo.getContactPerson() != null, "联系人不能为空");
        Preconditions.checkArgument(deliveryInfo.getDeliveryStatus() != null, "配送状态不能为空");

        DeliveryInfo info = new DeliveryInfo();
        BeanUtils.copyProperties(deliveryInfo, info);
        //雪花算法生成订单编号
        SnowflakeIdGenerator snowflakeIdGenerator = new SnowflakeIdGenerator(1, 1);
        info.setOrderNumber(snowflakeIdGenerator.nextId().toString());
        return this.save(info);
    }

    @Override
    public Boolean updates(DeliverInfoCreatOrUpdateParam deliveryInfo) {
        Preconditions.checkArgument(deliveryInfo.getId() != null, "id不能为空");
        DeliveryInfo info = new DeliveryInfo();
        BeanUtils.copyProperties(deliveryInfo, info);
        return this.updateById(info);
    }

    @Override
    public boolean removeByNumber(String orderNumber) {
        if (orderNumber != null) {
            LambdaUpdateWrapper<DeliveryInfo> wrapper = new LambdaUpdateWrapper<>();
            wrapper.eq(DeliveryInfo::getOrderNumber, orderNumber);
            return this.remove(wrapper);
        }
        return false;
    }


    @Override
    public IPage<DeliveryInfo> listByStatus(Integer status, Integer pageNum, Integer pageSize) {
        Preconditions.checkArgument(status != null, "配送状态不能为空");
        IPage<DeliveryInfo> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DeliveryInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeliveryInfo::getDeliveryStatus, status);
        return deliveryInfoMapper.selectPage(page, queryWrapper);
    }

    @Override
    public DeliveryInfo getByOrderNumber(String orderNumber) {
        Preconditions.checkArgument(orderNumber != null, "订单编号不能为空");
        LambdaQueryWrapper<DeliveryInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeliveryInfo::getOrderNumber, orderNumber);
        return getOne(queryWrapper);
    }
}



